var setting = {
	view: {
		dblClickExpand: false,
		showLine: false,
		selectedMulti: false
	},
	data: {
		simpleData: {
			enable:true,
			idKey: "id",
			pIdKey: "pId",
			rootPId: ""
		}
	},
	callback: {
		beforeClick: function(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("tree");
				demoIframe.attr("src",path+"/resource/resource_toedit?id="+treeNode.id);
				return true;

		}
	}
};

		
var code;
		
function showCode(str) {
	if (!code) code = $("#code");
	code.empty();
	code.append("<li>"+str+"</li>");
}

function deleteresource(){
	var zTree = parent.$.fn.zTree.getZTreeObj("treeDemo");
    var nodes = zTree.getSelectedNodes();
	if(nodes[0].children){
		layer.alert("请先删除下属机构！");
		return;
	}	
    layer.confirm("确定操作？",function(){
    	  $.ajax({
    			url : path+"/resource/resource_delete",
    			cache : false,
    			type : 'post',
    			dataType : 'html',
    			data :{"resourceId":JSON.stringify(nodes[0].id)},
    	        success: function(message){
    	    		if(message == "200"){
    	                layer.msg('操作成功!',{icon:1,time:1000},function(){
    	/*                         var index = parent.layer.getFrameIndex(window.name);
    	                    parent.query_brand_form.submit();
    	                    parent.layer.close(index); */
    	                    parent.window.location.reload();
    	                });
    					}else if(message == "500"){
    						layer.alert("系统繁忙！");
    					}else{
    						layer.alert(message);
    					}
    		    },
    		    error: function(){
                    layer.msg('操作失败!',{icon:5,time:1000});
    		    }
    		}); 
    });

}
function update(){

	var resourceId=$("#resourceId").val();
	var orgName =$("#name").val();
	var orgNon =$("#number").val();
	var orgUrl =$("#url").val();
	var orgCode=$("#resourceId").val();
	if(orgName.length==0 || trim(orgName).length == 0){
		alert("资源名称不能为空！");
		window.orgAdmin.document.getElementById("name").focus();
		return;
	}
	//验证只包含字母或者汉字
	var pattern =/(^[\u4e00-\u9fa5A-Za-z0-9_-]*$)/;
	//验证不能为纯数字
	var patternNum=/^\d+$/;
	if (!pattern.test(orgName)) {
		alert("请输入合法的资源名称！");
		return false;
	}
	if (patternNum.test(orgName)) {
		alert("资源名称不能为纯数字！");
		return false;
	}

	if(orgNon.length==0 || trim(orgNon).length == 0){
		alert("资源编号不能为空");
		return false;
	}


	//类型为资源的时候才检查请求地址
	if($('input:radio[name="type"]:checked').val()=="2"){
		var patternUrl=/(^[A-Za-z0-9:/.]*$)/;
		if(orgUrl.length==0 || trim(orgUrl).length == 0){
			alert("请求地址不能为空或全空格");
			return false;
		}
		if (patternNum.test(orgUrl)) {
			alert("请求地址不能为纯数字！");
			return false;
		}
	}
    layer.confirm("确定修改？",function(){
        $("#form-resource").ajaxSubmit({
    		dataType : 'html',
            success: function(message){
            		if(message == "200"){
                        layer.msg('操作成功!',{icon:1,time:1000},function(){
    /*                         var index = parent.layer.getFrameIndex(window.name);
                            parent.query_brand_form.submit();
                            parent.layer.close(index); */
                            parent.window.location.reload();
                        });
     				}else if(message == "500"){
     					layer.alert("系统繁忙！");
     				}else{
     					layer.alert(message);
     				}
            },
            error: function(){
                layer.msg('操作失败!',{icon:5,time:1000});
            }
        });
    });
}
function add(){
	var zTree = parent.$.fn.zTree.getZTreeObj("treeDemo");
    var nodes = zTree.getSelectedNodes();
    var id=JSON.stringify(nodes[0].id);
	layer_show('添加资源',path+'/resource/resource_toadd?id='+id,'800','600');
}
function save(){

	var resourceId=$("#resourceId").val();
	var orgName =$("#name").val();
	var orgNon =$("#number").val();
	var orgUrl =$("#url").val();
	var orgCode=$("#resourceId").val();
	if(orgName.length==0 || trim(orgName).length == 0){
		alert("资源名称不能为空！");
		window.orgAdmin.document.getElementById("name").focus();
		return;
	}
	//验证只包含字母或者汉字
	var pattern =/(^[\u4e00-\u9fa5A-Za-z0-9_-]*$)/;
	//验证不能为纯数字
	var patternNum=/^\d+$/;
	if (!pattern.test(orgName)) {
		alert("请输入合法的资源名称！");
		return false;
	}
	if (patternNum.test(orgName)) {
		alert("资源名称不能为纯数字！");
		return false;
	}

	if(orgNon.length==0 || trim(orgNon).length == 0){
		alert("资源编号不能为空");
		return false;
	}
	if (!patternNum.test(orgNon)) {
		alert("请输入正确的资源编号");
		return false;
	}


	//类型为资源的时候才检查请求地址
	if($('input:radio[name="type"]:checked').val()=="2"){
		var patternUrl=/(^[A-Za-z0-9:/.]*$)/;
		if(orgUrl.length==0 || trim(orgUrl).length == 0){
			alert("请求地址不能为空或全空格");
			return false;
		}
		if (patternNum.test(orgUrl)) {
			alert("请求地址不能为纯数字！");
			return false;
		}
	}
    $("#form-resource").ajaxSubmit({
		dataType : 'html',
        success: function(message){
        		if(message == "200"){
                    layer.msg('操作成功!',{icon:1,time:1000},function(){
/*                         var index = parent.layer.getFrameIndex(window.name);
                        parent.query_brand_form.submit();
                        parent.layer.close(index); */
                    	parent.parent.window.location.reload();
                    });
 				}else if(message == "500"){
 					layer.alert("系统繁忙！");
 				}else{
 					layer.alert(message);
 				}
        },
        error: function(){
            layer.msg('操作失败!',{icon:5,time:1000});
        }
    });
}
function trim(str){ 
	 return str.replace(/(^\s*)|(\s*$)/g, "");
	}